import PaBaseModel from '../../core/model/PaBaseModel';
import WebUploader from 'webuploader/dist/webuploader.js';

function equipmentInfoNewCtrl($scope, $http, ngDialog, fileUploaderService) {
  'ngInject';

  // 变量_this代表controllerAs 用于fileUploaderService
  let _this = this;

  // 表单数据
  this.editableEquipmentInfo = {
    id: '', // 主键
    deviceName: '', // 设备名称
    innerNo: '', // 档案编号
    deviceModel: '', // 设备型号
    registrationNo: '', // 注册号
    license: '', // 拍照
    deviceLevel: '', // 设备等级
    operator: '', // 操作人员
    approachTime: '', // 进场时间
    exitTime: '', // 退场时间
    maintainSituation: '', // 维保情况
    isRented: '', // 是否租赁 0 否: 1 是
  };


  this.editableEquipmentInfo = new PaBaseModel(this.editableEquipmentInfo);

  // 上传图片
  // 保存上传文件返回token
  this.avatarUploadToken = {}; // 头像图片token

  // 保存设备新增
  this.save = (param) => {
    // 修改表单是否提交过状态
    param.$submitted = true;

    // 表单校验
    if (param.$invalid) {
      return false;
    }

    // 把文件上传返回token传入this.userInfo
    this.editableEquipmentInfo.image = this.avatarUploadToken.id;
    $http.post(`${SERVER_CONTEXT}/api/deviceinfo/insertDeviceInfo`, 
      this.editableEquipmentInfo).
      then((resp) => {
        if (resp.data.resultCode == 0){
          layer.msg(resp.data.resultMsg);
          $state.go('equipmentInfoDetail', {id: resp.data.id});
        } else {
          layer.alert(resp.data.resultMsg);
        }
      });
  }

  // 重置设备新增
  this.reset = () => {
    this.editableEquipmentInfo = new PaBaseModel();
  }
}

export default equipmentInfoNewCtrl;